import requestBody from '@/api/apiBasics/requestBody';
import { isNullPm } from '@/utils/baseMethods';

const apix = new requestBody(
  {
    // baseURL: 'http://192.168.3.168:81/dev-api/',
    baseURL: '/print',
    timeout: 10000,
    headers: {
      'Content-Type': 'application/json;charset=UTF-8',
    },
  },
  (_res: any) => {},
  (_res: any) => {},
);
const { APIPOST } = apix;

const steamDom = {
  sn: '74702NWYPM6E14A',
  content: null,
  voice: 3,
  user: '1450168131@qq.com',
  timestamp: '1710150424',
  sign: '447086ddcb2a4e291668a550ec9f59c8bda76bd2',
};
/**
 * @exports printInit 打印入口
 * @param packet 数据包,数据已Object直接传递即可,数据包已JSON.stringify 转为字符串,扫码后直接再次转化即可
 * @param printNo 打印模板,传值string类型
 *                001 到货检验箱码标签模板
 * @param printNumber 打印数量类型为Int型
 */
const printInit = (packet: any, printNo: any, printNumber: any = 1): any => {
  if (isNullPm(packet)) {
    console.log('packet=', packet);
    console.log('printNo=', printNo);
    return false;
  }
  let printPacket: any = ``;
  for (let a = 1; a <= printNumber; a++) {
    if (printNo == '001') printPacket += printTeamplate001(packet, a) + '<BR>' + '----------------';
  }
  steamDom.content = printPacket;
  APIPOST('', steamDom);
};

/**
 * @exports printTeamplate001 到货检验箱码标签模板
 */
const printTeamplate001 = (packet, number) => {
  let steam = `<BR>编号:${number}<BR>`;
  Object.keys(packet).forEach((res: any) => {
    steam += packet[res] + '<BR>';
  });
  steam += `<QRCODE s=6 e=L l=center>${JSON.stringify(packet)}</QRCODE>`;
  return steam;
};

export default printInit;
